`timescale 1ps/1ps
`include "src/cpu_top.v"
module test_alu_top();
    
  reg clk, rst;
  integer counter = -1;
  integer i = 0;
  cpu_top U_CPU_TOP(clk, rst);

  initial begin
    clk <= 1;
    rst <= 0;
    #110 rst <= 1;
    #110 rst <= 0;
    $readmemh("./test_instr.dat", U_CPU_TOP.U_INSTR_MEM.IM);
    $display("data memory %x", U_CPU_TOP.U_INSTR_MEM.IM[0]);
    $display("--------------begin--------------\n");
    counter = 0;
  end

  always begin
    #(50) clk = ~clk;
    if(counter >= 18) $finish;
  end

  always @(negedge clk) begin
    #10
    if(counter >= 0) begin
      $display("# pc %x", U_CPU_TOP.pc);
      $display("|instr|func7|rs2|rs1|func3|rd|opcode|");
      $display("|-|-|-|-|-|-|-|");
      $display("|**%x**|%b|%b(%d)|%b(%d)|%b|%b(%d)|%b|", U_CPU_TOP.instr, U_CPU_TOP.func7, U_CPU_TOP.rs2, U_CPU_TOP.rs2, U_CPU_TOP.rs1, U_CPU_TOP.rs1, U_CPU_TOP.func3, U_CPU_TOP.rd, U_CPU_TOP.rd, U_CPU_TOP.opcode);
      $display("|**branch**|**mem_to_reg**|**mem_write**|**alu_src**|**reg_write**|**alu_op**|**imm_ctrl**|");
      $display("|%b|%b|%b|%b|%b|%b|%b|", U_CPU_TOP.branch, U_CPU_TOP.mem_to_reg, U_CPU_TOP.mem_write, U_CPU_TOP.alu_src, U_CPU_TOP.reg_write, U_CPU_TOP.alu_op, U_CPU_TOP.imm_ctrl);
      $display("|**imm**|**dm_read_data**|**alu_result**|**rf_write_data**|**rs2**|**rs1**|**rd**|");
      $display("|%d|%x|%x|%x|%d|%d|%d|", U_CPU_TOP.imm, U_CPU_TOP.dm_read_data, U_CPU_TOP.alu_result, U_CPU_TOP.rf_write_data, U_CPU_TOP.rs1, U_CPU_TOP.rs2, U_CPU_TOP.rd);
      $display();
      $display("|reg_file(i)|x(i)|x(i+1)|x(i+2)|x(i+3)|");
      $display("|-|-|-|-|-|");
      for(i=0; i<32; i=i+4)
        $display("|%d|%x|%x|%x|%x|", i, U_CPU_TOP.U_REG_FILES.regs[i], U_CPU_TOP.U_REG_FILES.regs[i+1], U_CPU_TOP.U_REG_FILES.regs[i+2], U_CPU_TOP.U_REG_FILES.regs[i+3]);
      $display("|-|-|-|-|-|");
      $display("|**data_memory(i)**|**+0**|**+4**|**+8**|**+c**|");
      for(i=0; i<12; i++)
        $display("|%x| %x| %x| %x| %x|", i*4*4, U_CPU_TOP.U_DATA_MEM.DM[i], U_CPU_TOP.U_DATA_MEM.DM[i+1], U_CPU_TOP.U_DATA_MEM.DM[i+2], U_CPU_TOP.U_DATA_MEM.DM[i+3]);
      // $display("<!-- slide -->");
      $display();
      counter = counter + 1;
    end;
  end

  initial
  begin            
      $dumpfile("test_alu_top.vcd");  //生成的vcd文件名称
      $dumpvars(0, test_alu_top);       //tb模块名称
  end

endmodule